﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using crm_library.DataAccess.TableReps;
using crm_library.DataAccess.ObjectReps;
using crm_library.DataAccess;
using System.Web.UI.WebControls;
using System.Runtime.Serialization;

namespace crm_dal.Objects
{
	[DataContractAttribute]
	public class Product : ObjectRepresentation
	{
		#region constructor
		public new const string TableName = "Product";
		public new const string ObjectName = "Product";

		public Product(int productID)
			: base(TableName, ObjectName)
		{
			ProductID = productID;
			Load();
		}

		public Product()
			: base(TableName, ObjectName)
		{

		}
		#endregion constructor

		#region properties
		[ListingFieldAttribute("Product ID")]
		[DataFieldAttribute("ProductID", false, true)]
		[DataMember]
		public int ProductID { get; set; }

		[ListingFieldAttribute("Product Name")]
		[DataFieldAttribute("ProductName", false)]
		[DataMember]
		public string ProductName { get; set; }

		[ListingFieldAttribute("Price")]
		[DataFieldAttribute("Price", false)]
		[DataMember]
		public decimal Price { get; set; }

		#endregion properties


		#region ObjectRepresentation implementations
		public override int GetPKValue()
		{
			return ProductID;
		}

		public override void SetPKValue(int pk)
		{
			ProductID = pk;
		}

		public override int GetNextPKValue()
		{
			int maxInt = 0;

			foreach (var item in DB.ReadObjectList(new Product()))
			{
				maxInt = Math.Max(maxInt, item.GetPKValue());
			}
			return maxInt + 1;
		}

		public override void Load(int pk)
		{
			SetPKValue(pk);
			DB.ReadObject(this, pk);
		}

		public override void Update()
		{
			DB.UpdateObject(this, GetPKValue());
		}

		public override void Delete(int pk)
		{
			DB.DeleteObject(this, pk);
		}

		public override void Create()
		{
			DB.CreateObject(this);
		}

		public override ListItem ToListItem()
		{
			return new ListItem(ProductName, GetPKValue().ToString());
		}

		#endregion ObjectRepresentation implementations

		public static List<Product> GetAll()
		{
			return DB.ReadObjectList(new Product());
		}

	}
}

